﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using System.Web.UI.WebControls;
using System.Web.UI;
using DropNet;
using umbraco.BusinessLogic;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Dropbox
{
    public class DropboxConfigControl : AbstractConfigControl
    {
        private DropboxProvider provider;
        private DropboxConfig config;
        private TextBox apiKeyTextBox = new TextBox();
        private TextBox apiSecretTextBox = new TextBox();
        private TextBox emailTextBox = new TextBox();
        private TextBox passwordTextBox = new TextBox();
        private Label authTokenLabel = new Label();
        private Label authSecretLabel = new Label();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                config.ApiKey = apiKeyTextBox.Text;
                config.ApiSecret = apiSecretTextBox.Text;

                if (!string.IsNullOrEmpty(emailTextBox.Text) &&
                    !string.IsNullOrEmpty(passwordTextBox.Text))
                {

                    var client = new DropNetClient(config.ApiKey, config.ApiSecret);

                    var login = client.Login(emailTextBox.Text, passwordTextBox.Text);
                    if (login != null)
                    {
                        config.AuthToken = authTokenLabel.Text = login.Token;
                        config.AuthSecret = authSecretLabel.Text = login.Secret;
                    }

                    emailTextBox.Text = "";
                    passwordTextBox.Text = "";
                }

                return config.SerializeToJson();
            }
        }

        public DropboxConfigControl(DropboxProvider provider, string options)
        {
            this.provider = provider;
            this.config = options.DeserializeJsonTo<DropboxConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                apiKeyTextBox.Text = config.ApiKey;
                apiSecretTextBox.Text = config.ApiSecret;
                authTokenLabel.Text = config.AuthToken;
                authSecretLabel.Text = config.AuthSecret;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            apiKeyTextBox.ID = "apiKeyTextBox";
            apiSecretTextBox.ID = "secretKeyTextBox";
            emailTextBox.ID = "emailTextBox";
            passwordTextBox.ID = "passwordTextBox";

            apiKeyTextBox.CssClass = "guiInputText guiInputStandardSize";
            apiSecretTextBox.CssClass = "guiInputText guiInputStandardSize";
            emailTextBox.CssClass = "guiInputText guiInputStandardSize";
            passwordTextBox.CssClass = "guiInputText guiInputStandardSize";

            passwordTextBox.TextMode = TextBoxMode.Password;

            Controls.Add(apiKeyTextBox);
            Controls.Add(apiSecretTextBox);
            Controls.Add(emailTextBox);
            Controls.Add(passwordTextBox);
            Controls.Add(authTokenLabel);
            Controls.Add(authSecretLabel);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("API Key:", "Please enter a valid Dropbox API key", apiKeyTextBox);
            writer.AddFormRow("API Secret:", "Please enter your Dropbox API secret", apiSecretTextBox);

            writer.AddFormRow("Email:", "Please enter your Dropbox email", emailTextBox);
            writer.AddFormRow("Password:", "Please enter your Dropbox password", passwordTextBox);

            writer.AddFormRow("Token:", "Your OAuth token", authTokenLabel);
            writer.AddFormRow("Secret:", "Your OAuth secret", authSecretLabel);
        }
    }
}